<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Module: ActiveResource::CustomMethods</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <meta http-equiv="Content-Script-Type" content="text/javascript" />
  <link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" />
  <script type="text/javascript">
  // <![CDATA[

  function popupCode( url ) {
    window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
  }

  function toggleCode( id ) {
    if ( document.getElementById )
      elem = document.getElementById( id );
    else if ( document.all )
      elem = eval( "document.all." + id );
    else
      return false;

    elemStyle = elem.style;
    
    if ( elemStyle.display != "block" ) {
      elemStyle.display = "block"
    } else {
      elemStyle.display = "none"
    }

    return true;
  }
  
  // Make codeblocks hidden by default
  document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
  
  // ]]>
  </script>

</head>
<body>



    <div id="classHeader">
        <table class="header-table">
        <tr class="top-aligned-row">
          <td><strong>Module</strong></td>
          <td class="class-name-in-header">ActiveResource::CustomMethods</td>
        </tr>
        <tr class="top-aligned-row">
            <td><strong>In:</strong></td>
            <td>
                <a href="../../files/vendor/rails/activeresource/lib/active_resource/custom_methods_rb.html">
                vendor/rails/activeresource/lib/active_resource/custom_methods.rb
                </a>
        <br />
            </td>
        </tr>

        </table>
    </div>
  <!-- banner header -->

  <div id="bodyContent">



  <div id="contextContent">

    <div id="description">
      <p>
A module to support custom REST methods and sub-resources, allowing you to
break out of the &quot;default&quot; REST methods with your own custom
resource requests. For example, say you use <a
href="../Rails.html">Rails</a> to expose a REST service and configure your
routes with:
</p>
<pre>
   map.resources :people, :new =&gt; { :register =&gt; :post },
                          :member =&gt; { :promote =&gt; :put, :deactivate =&gt; :delete }
                          :collection =&gt; { :active =&gt; :get }

 This route set creates routes for the following HTTP requests:

   POST    /people/new/register.xml # PeopleController.register
   PUT     /people/1/promote.xml    # PeopleController.promote with :id =&gt; 1
   DELETE  /people/1/deactivate.xml # PeopleController.deactivate with :id =&gt; 1
   GET     /people/active.xml       # PeopleController.active
</pre>
<p>
Using this module, Active Resource can use these custom REST methods just
like the standard methods.
</p>
<pre>
  class Person &lt; ActiveResource::Base
    self.site = &quot;http://37s.sunrise.i:3000&quot;
  end

  Person.new(:name =&gt; 'Ryan).post(:register)  # POST /people/new/register.xml
  # =&gt; { :id =&gt; 1, :name =&gt; 'Ryan' }

  Person.find(1).put(:promote, :position =&gt; 'Manager') # PUT /people/1/promote.xml
  Person.find(1).delete(:deactivate) # DELETE /people/1/deactivate.xml

  Person.get(:active)  # GET /people/active.xml
  # =&gt; [{:id =&gt; 1, :name =&gt; 'Ryan'}, {:id =&gt; 2, :name =&gt; 'Joe'}]
</pre>

    </div>


   </div>

    <div id="method-list">
      <h3 class="section-bar">Methods</h3>

      <div class="name-list">
      <a href="#M001194">delete</a>&nbsp;&nbsp;
      <a href="#M001191">get</a>&nbsp;&nbsp;
      <a href="#M001190">included</a>&nbsp;&nbsp;
      <a href="#M001192">post</a>&nbsp;&nbsp;
      <a href="#M001193">put</a>&nbsp;&nbsp;
      </div>
    </div>

  </div>


    <!-- if includes -->
    <div id="includes">
      <h3 class="section-bar">Included Modules</h3>

      <div id="includes-list">
        <span class="include-name"><a href="CustomMethods/InstanceMethods.html">ActiveResource::CustomMethods::InstanceMethods</a></span>
      </div>
    </div>

    <div id="section">

    <div id="class-list">
      <h3 class="section-bar">Classes and Modules</h3>

      Module <a href="CustomMethods/ClassMethods.html" class="link">ActiveResource::CustomMethods::ClassMethods</a><br />
Module <a href="CustomMethods/InstanceMethods.html" class="link">ActiveResource::CustomMethods::InstanceMethods</a><br />

    </div>


    <div id="aliases-list">
      <h3 class="section-bar">External Aliases</h3>

      <div class="name-list">
                        <table summary="aliases">
        <tr class="top-aligned-row context-row">
          <td class="context-item-name">delete</td>
          <td>-&gt;</td>
          <td class="context-item-value">orig_delete</td>
        </tr>
                        </table>
      </div>
    </div>


      


    <!-- if method_list -->
    <div id="methods">
      <h3 class="section-bar">Public Class methods</h3>

      <div id="method-M001194" class="method-detail">
        <a name="M001194"></a>

        <div class="method-heading">
          <a href="#M001194" class="method-signature">
          <span class="method-name">delete</span><span class="method-args">(custom_method_name, options = {})</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001194-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001194-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activeresource/lib/active_resource/custom_methods.rb, line 68</span>
68:           <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">delete</span>(<span class="ruby-identifier">custom_method_name</span>, <span class="ruby-identifier">options</span> = {})
69:             <span class="ruby-comment cmt"># Need to jump through some hoops to retain the original class 'delete' method</span>
70:             <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">custom_method_name</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Symbol</span>)
71:               <span class="ruby-identifier">connection</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-identifier">custom_method_collection_url</span>(<span class="ruby-identifier">custom_method_name</span>, <span class="ruby-identifier">options</span>), <span class="ruby-identifier">headers</span>)
72:             <span class="ruby-keyword kw">else</span>
73:               <span class="ruby-identifier">orig_delete</span>(<span class="ruby-identifier">custom_method_name</span>, <span class="ruby-identifier">options</span>)
74:             <span class="ruby-keyword kw">end</span>
75:           <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001191" class="method-detail">
        <a name="M001191"></a>

        <div class="method-heading">
          <a href="#M001191" class="method-signature">
          <span class="method-name">get</span><span class="method-args">(custom_method_name, options = {})</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Invokes a GET to a given custom REST method. For example:
</p>
<pre>
  Person.get(:active)  # GET /people/active.xml
  # =&gt; [{:id =&gt; 1, :name =&gt; 'Ryan'}, {:id =&gt; 2, :name =&gt; 'Joe'}]

  Person.get(:active, :awesome =&gt; true)  # GET /people/active.xml?awesome=true
  # =&gt; [{:id =&gt; 1, :name =&gt; 'Ryan'}]
</pre>
<p>
Note: the objects returned from this method are not automatically converted
into <a href="Base.html">ActiveResource::Base</a> instances - they are
ordinary Hashes. If you are expecting <a
href="Base.html">ActiveResource::Base</a> instances, use the <tt>find</tt>
class method with the <tt>:from</tt> option. For example:
</p>
<pre>
  Person.find(:all, :from =&gt; :active)
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001191-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001191-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activeresource/lib/active_resource/custom_methods.rb, line 56</span>
56:           <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">get</span>(<span class="ruby-identifier">custom_method_name</span>, <span class="ruby-identifier">options</span> = {})
57:             <span class="ruby-identifier">connection</span>.<span class="ruby-identifier">get</span>(<span class="ruby-identifier">custom_method_collection_url</span>(<span class="ruby-identifier">custom_method_name</span>, <span class="ruby-identifier">options</span>), <span class="ruby-identifier">headers</span>)
58:           <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001190" class="method-detail">
        <a name="M001190"></a>

        <div class="method-heading">
          <a href="#M001190" class="method-signature">
          <span class="method-name">included</span><span class="method-args">(base)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001190-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001190-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activeresource/lib/active_resource/custom_methods.rb, line 34</span>
34:     <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">included</span>(<span class="ruby-identifier">base</span>)
35:       <span class="ruby-identifier">base</span>.<span class="ruby-identifier">class_eval</span> <span class="ruby-keyword kw">do</span>
36:         <span class="ruby-identifier">extend</span> <span class="ruby-constant">ActiveResource</span><span class="ruby-operator">::</span><span class="ruby-constant">CustomMethods</span><span class="ruby-operator">::</span><span class="ruby-constant">ClassMethods</span>
37:         <span class="ruby-identifier">include</span> <span class="ruby-constant">ActiveResource</span><span class="ruby-operator">::</span><span class="ruby-constant">CustomMethods</span><span class="ruby-operator">::</span><span class="ruby-constant">InstanceMethods</span>
38: 
39:         <span class="ruby-keyword kw">class</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-keyword kw">self</span>
40:           <span class="ruby-keyword kw">alias</span> <span class="ruby-identifier">:orig_delete</span> <span class="ruby-identifier">:delete</span>
41: 
42:           <span class="ruby-comment cmt"># Invokes a GET to a given custom REST method. For example:</span>
43:           <span class="ruby-comment cmt">#</span>
44:           <span class="ruby-comment cmt">#   Person.get(:active)  # GET /people/active.xml</span>
45:           <span class="ruby-comment cmt">#   # =&gt; [{:id =&gt; 1, :name =&gt; 'Ryan'}, {:id =&gt; 2, :name =&gt; 'Joe'}]</span>
46:           <span class="ruby-comment cmt">#</span>
47:           <span class="ruby-comment cmt">#   Person.get(:active, :awesome =&gt; true)  # GET /people/active.xml?awesome=true</span>
48:           <span class="ruby-comment cmt">#   # =&gt; [{:id =&gt; 1, :name =&gt; 'Ryan'}]</span>
49:           <span class="ruby-comment cmt">#</span>
50:           <span class="ruby-comment cmt"># Note: the objects returned from this method are not automatically converted</span>
51:           <span class="ruby-comment cmt"># into ActiveResource::Base instances - they are ordinary Hashes. If you are expecting</span>
52:           <span class="ruby-comment cmt"># ActiveResource::Base instances, use the &lt;tt&gt;find&lt;/tt&gt; class method with the</span>
53:           <span class="ruby-comment cmt"># &lt;tt&gt;:from&lt;/tt&gt; option. For example:</span>
54:           <span class="ruby-comment cmt">#</span>
55:           <span class="ruby-comment cmt">#   Person.find(:all, :from =&gt; :active)</span>
56:           <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">get</span>(<span class="ruby-identifier">custom_method_name</span>, <span class="ruby-identifier">options</span> = {})
57:             <span class="ruby-identifier">connection</span>.<span class="ruby-identifier">get</span>(<span class="ruby-identifier">custom_method_collection_url</span>(<span class="ruby-identifier">custom_method_name</span>, <span class="ruby-identifier">options</span>), <span class="ruby-identifier">headers</span>)
58:           <span class="ruby-keyword kw">end</span>
59: 
60:           <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">post</span>(<span class="ruby-identifier">custom_method_name</span>, <span class="ruby-identifier">options</span> = {}, <span class="ruby-identifier">body</span> = <span class="ruby-value str">''</span>)
61:             <span class="ruby-identifier">connection</span>.<span class="ruby-identifier">post</span>(<span class="ruby-identifier">custom_method_collection_url</span>(<span class="ruby-identifier">custom_method_name</span>, <span class="ruby-identifier">options</span>), <span class="ruby-identifier">body</span>, <span class="ruby-identifier">headers</span>)
62:           <span class="ruby-keyword kw">end</span>
63: 
64:           <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">put</span>(<span class="ruby-identifier">custom_method_name</span>, <span class="ruby-identifier">options</span> = {}, <span class="ruby-identifier">body</span> = <span class="ruby-value str">''</span>)
65:             <span class="ruby-identifier">connection</span>.<span class="ruby-identifier">put</span>(<span class="ruby-identifier">custom_method_collection_url</span>(<span class="ruby-identifier">custom_method_name</span>, <span class="ruby-identifier">options</span>), <span class="ruby-identifier">body</span>, <span class="ruby-identifier">headers</span>)
66:           <span class="ruby-keyword kw">end</span>
67: 
68:           <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">delete</span>(<span class="ruby-identifier">custom_method_name</span>, <span class="ruby-identifier">options</span> = {})
69:             <span class="ruby-comment cmt"># Need to jump through some hoops to retain the original class 'delete' method</span>
70:             <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">custom_method_name</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Symbol</span>)
71:               <span class="ruby-identifier">connection</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-identifier">custom_method_collection_url</span>(<span class="ruby-identifier">custom_method_name</span>, <span class="ruby-identifier">options</span>), <span class="ruby-identifier">headers</span>)
72:             <span class="ruby-keyword kw">else</span>
73:               <span class="ruby-identifier">orig_delete</span>(<span class="ruby-identifier">custom_method_name</span>, <span class="ruby-identifier">options</span>)
74:             <span class="ruby-keyword kw">end</span>
75:           <span class="ruby-keyword kw">end</span>
76:         <span class="ruby-keyword kw">end</span>
77:       <span class="ruby-keyword kw">end</span>
78:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001192" class="method-detail">
        <a name="M001192"></a>

        <div class="method-heading">
          <a href="#M001192" class="method-signature">
          <span class="method-name">post</span><span class="method-args">(custom_method_name, options = {}, body = '')</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001192-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001192-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activeresource/lib/active_resource/custom_methods.rb, line 60</span>
60:           <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">post</span>(<span class="ruby-identifier">custom_method_name</span>, <span class="ruby-identifier">options</span> = {}, <span class="ruby-identifier">body</span> = <span class="ruby-value str">''</span>)
61:             <span class="ruby-identifier">connection</span>.<span class="ruby-identifier">post</span>(<span class="ruby-identifier">custom_method_collection_url</span>(<span class="ruby-identifier">custom_method_name</span>, <span class="ruby-identifier">options</span>), <span class="ruby-identifier">body</span>, <span class="ruby-identifier">headers</span>)
62:           <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001193" class="method-detail">
        <a name="M001193"></a>

        <div class="method-heading">
          <a href="#M001193" class="method-signature">
          <span class="method-name">put</span><span class="method-args">(custom_method_name, options = {}, body = '')</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001193-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001193-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activeresource/lib/active_resource/custom_methods.rb, line 64</span>
64:           <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">put</span>(<span class="ruby-identifier">custom_method_name</span>, <span class="ruby-identifier">options</span> = {}, <span class="ruby-identifier">body</span> = <span class="ruby-value str">''</span>)
65:             <span class="ruby-identifier">connection</span>.<span class="ruby-identifier">put</span>(<span class="ruby-identifier">custom_method_collection_url</span>(<span class="ruby-identifier">custom_method_name</span>, <span class="ruby-identifier">options</span>), <span class="ruby-identifier">body</span>, <span class="ruby-identifier">headers</span>)
66:           <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>


    </div>


  </div>


<div id="validator-badges">
  <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
</div>

</body>
</html>